﻿package core.scene {
	import flash.display.MovieClip;
	import base.BaseMc;
	import core.display.BookCase;
	import core.display.MainMenu;
	import core.events.CmdEvent;
	import core.net.socket.cmd.GetBarCodeCmd;
	import utils.UtilScreen;
	import core.log.Logger;
	import core.events.MainMenuEvent;
	import core.net.socket.cmd.BluetoothDisConnectCmd;
	import core.dialog.DialogMessage;
	import core.events.MessageDialogEvent;
	import core.net.socket.cmd.LogoutCmd;
	import core.events.BookCaseEvent;
	import core.events.BgSoundEvent;

	public class SceneHall extends BaseMc{
		
		private var bookCase:BookCase;
		private var menuRight:MainMenu;
		private var menuLeft:MainMenu;
		
		public function SceneHall() {
			
		}
		
		
		protected override function onInit():void {
			super.onInit();
			this.dispatchEvent(new BgSoundEvent(BgSoundEvent.START));
			
			this.initBookCase();
			this.initMenuButton();
			
			this.addEventListener(BookCaseEvent.TOP_CLICKED,onTopClick);//书架排行榜点击事件
			this.addEventListener(BookCaseEvent.CLASS_CLICKED,onClassClick);//书架魔法课堂点击事件
		}
		
		protected override function onRemove():void {
			super.onRemove();
			this.removeEventListener(BookCaseEvent.TOP_CLICKED,onTopClick);//书架排行榜点击事件
			this.removeEventListener(BookCaseEvent.CLASS_CLICKED,onClassClick);//书架魔法课堂点击事件
		}
		
		//初始化书架元件
		private function initBookCase():void {
			this.bookCase = new BookCase();
			this.bookCase.x = 50;
			this.bookCase.y = 100;
			this.addChild(this.bookCase);
		}
		
		
		
		//初始化按钮元件
		private function initMenuButton():void {
			
			//var screenHeight = UtilScreen.getScreenHeight();
			//var screenWidth = UtilScreen.getScreenWidth();
			var screenHeight = this.stage.stageHeight;
			var screenWidth = this.stage.stageWidth;
			trace("screenHeight:" + screenHeight);
			trace("screenWidth:"+screenWidth);
			
			//右边位置
			this.menuRight = new MainMenu();
			this.menuRight.scaleX = this.menuRight.scaleX * 0.7;
			this.menuRight.scaleY = this.menuRight.scaleY * 0.7;
			this.menuRight.y = screenHeight - 120;
			this.menuRight.x = screenWidth - 120;
			this.menuRight.addEventListener(MainMenuEvent.LOGOUT,onLogout);
			this.addChild(this.menuRight);
			
			//左边位置
			this.menuLeft = new MainMenu();
			this.menuLeft.scaleX = this.menuLeft.scaleX * 0.7;
			this.menuLeft.scaleY = this.menuLeft.scaleY * 0.7;
			this.menuLeft.y = screenHeight - 120;
			this.menuLeft.x = 50;
			this.menuLeft.addEventListener(MainMenuEvent.LOGOUT,onLogout);
			this.addChild(this.menuLeft);
			
		}
		
		private function onLogout(e:MainMenuEvent):void {
			var dialogMessage:DialogMessage = new DialogMessage("确定退出吗",false);
			dialogMessage.addEventListener(MessageDialogEvent.OK,onUserLogout);
			this.addChild(dialogMessage);
		}
		
		private function onTopClick(e:BookCaseEvent):void {
			var rankScene:SceneRank = new SceneRank(this);
			this.parent.addChild(rankScene);
			this.hide();
		}
		
		private function onClassClick(e:BookCaseEvent):void {
			var classScene:SceneClass = new SceneClass(this);
			this.parent.addChild(classScene);
			this.hide();
		}
		
		
		private function onUserLogout(e:MessageDialogEvent):void {
			//退出登陆
			//this.sendCmd(LogoutCmd.make());
			//断开蓝牙连接
			this.sendCmd(BluetoothDisConnectCmd.make());
			var loginScene:SceneLogin = new SceneLogin(true);
			this.parent.addChild(loginScene);
			this.remove();
		}
		
		protected override function onCommandResponse(e:CmdEvent):void{
			super.onCommandResponse(e);
			var data:Object = e.data;
			if(data.cmd == LogoutCmd.COMMAND_ID){
				if(data.error != ""){
					
				}
				
			}
		}
		
	}
	
}
